package com.west_traveler.find_page_fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.CategorieRadio;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.FirstData;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.LiveData;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.LocalRadio;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.TopRadio;
import com.west_traveler.recyclerview_adapter.LiveFragmentRecyclerViewAdapter;
import com.west_traveler.ximalayafm.MainActivity;
import com.west_traveler.ximalayafm.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*
* 
* @author Wang_JunJie
* @date 2016/12/3
*
*/


public class LiveFragment extends Fragment implements Callback<FirstData> {


    private RecyclerView recycler_frg_live;

    public static LiveFragment getInstance(){
        return new LiveFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_live, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler_frg_live = ((RecyclerView) view.findViewById(R.id.recycle_frg_live_id));
        //数据源
        ((BaseApplication) getActivity().getApplication())
                .getLiveHttpService()
                .getLiveData()
                .enqueue(this);
    }

    @Override
    public void onResponse(Call<FirstData> call, Response<FirstData> response) {

        //数据源
        LiveData data = response.body().getData();
        List<CategorieRadio> categories = data.getCategories();
        List<LocalRadio> localRadios = data.getLocalRadios();
        String location =data.getLocation();
        List<TopRadio> topRadios = data.getTopRadios();
        //适配器
        LiveFragmentRecyclerViewAdapter adapter = new LiveFragmentRecyclerViewAdapter(getContext(), categories, localRadios, location, topRadios,(MainActivity)getActivity());

        //绑定适配器
        recycler_frg_live.setAdapter(adapter);

        //设置item之间的距离
        recycler_frg_live.addItemDecoration(new SpaceItemDecoration(30));
    }

    @Override
    public void onFailure(Call<FirstData> call, Throwable t) {
    }

    //ItemDecoration自定义子类
    private class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (parent.getChildAdapterPosition(view)!=0) {
                outRect.bottom = space;
            }
        }


    }
}
